You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add shallow equality guards to MessageBlockStore to prevent unnecessary renders
Summary
• In cli/src/state/message-block-store.ts, added shallow equality guards to setContext and setCallbacks to prevent state reference churn and eliminate spurious notifications to store subscribers and context consumers.
• Previously, setContext (triggered on every layout effect in <Chat />) drafted and replaced state.context with a new object reference even when all property values were identical. Because Zustand notifies store subscribers whenever a new state reference is created by Immer, this triggered re-render passes for any components and listeners consuming the store or context directly.
• In setContext, added a key-by-key shallow equality guard for merged properties.
• In setCallbacks, respected full replacement semantics by checking key set length before checking individual values, ensuring that omitted callback keys trigger replacement rather than being silently preserved.
• Exported and shared a single EMPTY_RESPONSE_ADS module constant between initialContext and chat.tsx, ensuring stable object reference identity when ads are disabled with zero allocation.
• Added unit test suite in cli/src/state/__tests__/message-block-store.test.ts verifying that subscribers fire only on true mutations, are suppressed on identical updates, and handle omitted keys in setCallbacks.
Test plan
[✓] bun test --config=/dev/null src/state/__tests__/message-block-store.test.ts — 4 pass, 0 fail
[✓] bun run --cwd cli typecheck — 0 errors
[✓] PR hygiene check passed
Good catch on the render-churn issue — setContext unconditionally created a new context object on every layout effect, and Immer/Zustand notified subscribers even when nothing changed. The guard logic is sound: setContext bails out via an early return inside the Immer producer when no updated key actually differs, and setCallbacks preserves 'full replacement' semantics by checking key-count first before comparing values, so omitted keys are correctly treated as changes. The EMPTY_RESPONSE_ADS constant is a nice touch to avoid a fresh {} allocation feeding into the equality check on every render.
A few things worth polishing before this lands:
The key-set comparison in setCallbacks only checks prevKeys.length !== nextKeys.length, not actual set equality. It happens to work because any differing key pair with equal counts still produces a value mismatch on the iterated key, but it's fragile — a small comment explaining why length-check + per-key diff is sufficient would help future readers, or just do a proper key-set diff.
Consider whether this shallow-equality pattern belongs in a shared helper (e.g. zustand/shallow or a local shallowEqual util) rather than being hand-rolled twice in this file, since other stores in cli/src/state likely have the same churn problem.
Tests are solid and cover the four cases described in the PR body — appreciated for a first-time contributor.
Overall this is a small, well-scoped, testable fix in cli/src/state and chat.tsx, not touching any restricted paths. Worth porting after a maintainer double-checks the key-set comparison edge case.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add shallow equality guards to MessageBlockStore to prevent unnecessary renders
Summary
• In
cli/src/state/message-block-store.ts, added shallow equality guards tosetContextandsetCallbacksto prevent state reference churn and eliminate spurious notifications to store subscribers and context consumers.• Previously,
setContext(triggered on every layout effect in<Chat />) drafted and replacedstate.contextwith a new object reference even when all property values were identical. Because Zustand notifies store subscribers whenever a new state reference is created by Immer, this triggered re-render passes for any components and listeners consuming the store or context directly.• In
setContext, added a key-by-key shallow equality guard for merged properties.• In
setCallbacks, respected full replacement semantics by checking key set length before checking individual values, ensuring that omitted callback keys trigger replacement rather than being silently preserved.• Exported and shared a single
EMPTY_RESPONSE_ADSmodule constant betweeninitialContextandchat.tsx, ensuring stable object reference identity when ads are disabled with zero allocation.• Added unit test suite in
cli/src/state/__tests__/message-block-store.test.tsverifying that subscribers fire only on true mutations, are suppressed on identical updates, and handle omitted keys insetCallbacks.Test plan
[✓]
bun test --config=/dev/null src/state/__tests__/message-block-store.test.ts— 4 pass, 0 fail[✓]
bun run --cwd cli typecheck— 0 errors[✓] PR hygiene check passed